package com.qiche.controller.mgr;

import com.qiche.aspect.Result;
import com.qiche.constant.SubThreshold;
import com.qiche.dto.IntroductionDto;
import com.qiche.dto.ThresholdDto;
import com.qiche.model.Merchant;
import com.qiche.model.SpecialOffer;
import com.qiche.model.SubscribeConfig;
import com.qiche.model.User;
import com.qiche.service.SpecialOfferService;
import com.qiche.service.SubscribeConfigService;
import com.qiche.util.UserUtils;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 预约设置控制器
 */
@RestController
@RequestMapping("/subscribeConfig")
public class SubscribeConfigController {
    @Autowired
    private SubscribeConfigService subscribeConfigService;

    /**
     * 设置繁忙程度阈值
     *
     * @param thresholdDto 1：空闲 2:适中 3:紧张 4：火爆
     * @return
     */
    @PostMapping("/setThreshold")
    public Result setThreshold(ThresholdDto thresholdDto) {

        SubscribeConfig subscribeConfig = subscribeConfigService.selectByMerchantId(UserUtils.getCurrentUser().getMerchantId());

        Integer degreeHot = subscribeConfig.getDegreeHot();
        Integer degreeTense = subscribeConfig.getDegreeTense();
        Integer degreeModerate = subscribeConfig.getDegreeModerate();
        Integer degreeLeisure = subscribeConfig.getDegreeLeisure();

        if (thresholdDto.getType().equals(SubThreshold.HOT)) {
            if (degreeTense!=null && thresholdDto.getValue() <= degreeTense) {
                return Result.createByErrorMessage("繁忙程度阈值必须大于紧张程度");
            }
        } else if (thresholdDto.getType().equals(SubThreshold.TENSE)) {
            if (degreeHot!=null && thresholdDto.getValue() >= degreeHot) {
                return Result.createByErrorMessage("紧张程度阈值必须小于繁忙程度");
            }
            if (degreeModerate!=null && thresholdDto.getValue() <= degreeModerate) {
                return Result.createByErrorMessage("紧张程度阈值必须大于适中程度");
            }

        } else if (thresholdDto.getType().equals(SubThreshold.MODERATE)) {
            if (degreeTense!=null && thresholdDto.getValue() >= degreeTense) {
                return Result.createByErrorMessage("适中程度阈值必须小于紧张程度");
            }
            if (degreeLeisure!=null && thresholdDto.getValue() <= degreeLeisure) {
                return Result.createByErrorMessage("适中程度阈值必须大于空闲程度");
            }

        } else if (thresholdDto.getType().equals(SubThreshold.LEISURE)) {
            if (degreeModerate!=null && thresholdDto.getValue() >= degreeModerate) {
                return Result.createByErrorMessage("空闲程度阈值必须小于适中程度");
            }

        }
        //验证通过
        if (thresholdDto.getType().equals(SubThreshold.HOT)) {
            subscribeConfig.setDegreeHot(thresholdDto.getValue());
        } else if (thresholdDto.getType().equals(SubThreshold.TENSE)) {
            subscribeConfig.setDegreeTense(thresholdDto.getValue());
        } else if (thresholdDto.getType().equals(SubThreshold.MODERATE)) {
            subscribeConfig.setDegreeModerate(thresholdDto.getValue());
        } else if (thresholdDto.getType().equals(SubThreshold.LEISURE)) {
            subscribeConfig.setDegreeLeisure(thresholdDto.getValue());
        }
        int updateCount = subscribeConfigService.updateNotNull(subscribeConfig);
        if (updateCount > 0) {
            return Result.createBySuccessMessage("修改成功");
        }
        return Result.createByErrorMessage("修改失败");
    }

    @ApiOperation(value = "获取门店预约阈值情况")
    @GetMapping(value = "/getThreshold")
    public Result getThreshold() {
        User user = UserUtils.getCurrentUser();
        SubscribeConfig config = subscribeConfigService.selectByMerchantId(user.getMerchantId());
        return Result.createBySuccess(config);
    }

    @ApiOperation(value = "设置预约设置启用状态")
    @PostMapping(value = "/setAlwaysStatus")
    public Result setAlwaysStatus(Integer status){

        User user = UserUtils.getCurrentUser();
        SubscribeConfig config = subscribeConfigService.selectByMerchantId(user.getMerchantId());
        config.setAlwaysStatus(status);
        int updateCount=  subscribeConfigService.updateNotNull(config);
        if(updateCount>0){
            return Result.createBySuccessMessage("设置成功");
        }
        return Result.createByErrorMessage("设置失败");
    }
}
